import React from "react";
import type { DataAction, IProTableRef } from "@yaf/pro-ui";
import { getPostPageList, createPost, updatePostInfo, deletePostInfo, getPostInfo } from "../../api";

export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>): DataAction => ({
  fetchList: (params) => {
    const { page, pageSize, filter } = params;
    return getPostPageList(Object.assign({ page, pageSize }, filter || {})).then((resp) => resp.data)
  },
  getItem: (rowKey) => {
    const postId = rowKey as number;
    return getPostInfo(postId).then((resp) => {
      const { postCode, postName, postSort, status, remark } = resp.data;
      return { postCode, postName, postSort, status, remark }
    });
  },
  createItem: (params) => {
    return createPost(params).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  updateItem: (rowKey, params) => {
    params.postId = rowKey;
    return updatePostInfo(params).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  deleteItems: (selectedRowKeys) => {
    return deletePostInfo(selectedRowKeys.map((item) => parseInt(item)))
  },
})


